#! /usr/bin/env bash

#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

COUNT=${COUNT:-10}
TSQA_TSXS=${TSQA_TSXS:-/opt/ats/bin/tsxs}
TSQA_TESTNAME=$(basename $0)
source $(dirname $0)/functions

# Make up an arbitratry URL to generate error logs. We use the loopback address
# number because I've seen "localhost" resolve to an IPv6 address. That sucked.
URL=http://127.0.0.1:$PORT

# This test verifies that online reloading of the logging configuration
# does not crash Traffic Server.
check() {
  local logdir=$(logdir)

  for i in $(seq $COUNT) ; do
    msg check $i ...
    curl --silent --show-error -o /dev/null -H "Host: not there" $URL
    tsexec traffic_line -s proxy.config.diags.debug.tags -v 'log-.*'
    tsexec traffic_line -s proxy.config.diags.debug.enabled -v 1
    tsexec traffic_line -x
    msgwait 2
    curl --silent --show-error -o /dev/null -H "Host: not there" $URL
    # Set a harmless proxy.config.log.hostname to trigger a config reload ...
    tsexec traffic_line -s proxy.config.log.hostname -v jpeach-test-$$-$i
    tsexec traffic_line -s proxy.config.log.search_top_sites -v $i
    tsexec traffic_line -x
    msgwait 2
    curl --silent --show-error -o /dev/null -H "Host: not there" $URL
    crash

    # Verify that we have all the error logs that we expect.
    for logfile in error.log diags.log manager.log traffic.out ; do
      [[ -e $TSQA_ROOT/$logdir/$logfile ]] || fatal $logfile is missing
    done

  done
}

bootstrap

# If Traffic Server is not up, bring it up ...
alive cop || startup || fatal unable to start Traffic Server
trap shutdown 0 EXIT

msgwait 6
check

exit $TSQA_FAIL

# vim: set sw=2 ts=2 et :
